package com.fuershiye.woman.monitor.dao.admin;

import com.fuershiye.woman.monitor.beans.Page;
import com.fuershiye.woman.monitor.beans.ViewUser;
import com.fuershiye.woman.monitor.beans.ViewUserTel;
import com.fuershiye.woman.monitor.dao.HibernateDao;
import com.fuershiye.woman.monitor.model.User;
import com.fuershiye.woman.monitor.utils.DaoSqlUtil;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.math.BigInteger;
import java.util.List;

@Repository("adminUserDao")
@SuppressWarnings("unchecked")
public class UserDao {

    @Autowired
    private HibernateDao hibernateDao;

    public List<ViewUserTel> getSameAreaUserByAreaid(Long areaid) {
        String sql = "SELECT u.id,u.user_name,u.name as uname,u.tel,u.status,d.name as dname, r.name as rname FROM `admin_user` as u left join `admin_user_role` as ur on u.id = ur.uid left join `admin_role` as r on r.id = ur.rid left join `department` as d on d.id = r.department WHERE d.area =? order by d.id ASC";
        Query query = hibernateDao.getSqlQuery(sql);
        query.setLong(0, areaid);
        List<Object[]> queryResult = query.list();
        List<ViewUserTel> result = Lists.newArrayList();
        for (Object[] fields : queryResult) {
            result.add(query2ViewUserTel(fields));
        }
        return result;
    }

    public User findByName(String username) {
        String sql = "select * from admin_user where user_name = ?";
        SQLQuery query = hibernateDao.getSqlQuery(sql).addEntity(User.class);
        query.setString(0, username);
        return (User)query.uniqueResult();
    }
    public User getByTel(String tel) {
        String sql = "select * from admin_user where tel = ?";
        SQLQuery query = hibernateDao.getSqlQuery(sql).addEntity(User.class);
        query.setString(0, tel);
        return (User)query.uniqueResult();
    }
    
    public void save(User admin){
        hibernateDao.save(admin);
    }

    @SuppressWarnings("unchecked")
	public List<ViewUser> listByCondition(String user_name, Integer status, Page page) {
        StringBuffer sb = new StringBuffer("select id,user_name,email,type,tel,status from admin_user where 1=1 ");
        sb = DaoSqlUtil.appendSql(sb, "status", status);
        if(StringUtils.isNotBlank(user_name)){
            sb.append("and user_name like:user_name ");
        }
        Query query = hibernateDao.getSqlQuery(sb.toString());
        query = DaoSqlUtil.appendQuery(query, "status", status);
        if(StringUtils.isNotBlank(user_name)){
            query.setString("user_name","%" + user_name + "%");
        }
        query.setFirstResult(page.getStart());
        query.setMaxResults(page.getPageSize());
        List<ViewUser> result = Lists.newArrayList();
        List<Object[]> queryResult = query.list();
        for (Object[] fields : queryResult) {
            result.add(query2ViewUser(fields));
        }
        return result;
    }

    public int countByCondition(String user_name, Integer status) {
        StringBuffer sb = new StringBuffer("select count(*) from admin_user where 1=1 ");
        sb = DaoSqlUtil.appendSql(sb, "status", status);
        if(StringUtils.isNotBlank(user_name)){
            sb.append("and user_name like:user_name ");
        }
        Query query = hibernateDao.getSqlQuery(sb.toString());
        query = DaoSqlUtil.appendQuery(query, "status", status);
        if(StringUtils.isNotBlank(user_name)){
            query.setString("user_name","%" + user_name + "%");
        }
        BigInteger count = (BigInteger) query.uniqueResult();
        return (int) count.longValue();
    }

    public User get(Long id) {
        return hibernateDao.getById(User.class,id);
    }

    private ViewUser query2ViewUser(Object[] fields){
        ViewUser viewUser = new ViewUser();
        viewUser.setId((fields[0] == null) ? 0L:Long.parseLong(fields[0].toString()));
        viewUser.setName(fields[1] == null? "":String.valueOf(fields[1].toString()));
        viewUser.setEmail(fields[2] == null? "":String.valueOf(fields[2].toString()));
        viewUser.setType((fields[3] == null) ? 0:Integer.parseInt(fields[3].toString()));
        viewUser.setTel(fields[4] == null? "":String.valueOf(fields[4].toString()));
        viewUser.setStatus(fields[5] == null? 0:Integer.parseInt(fields[5].toString()));
        return viewUser;
    }
    private ViewUserTel query2ViewUserTel(Object[] fields){
        ViewUserTel viewUser = new ViewUserTel();
//        u.id,u.user_name,u.tel,u.`status`,d.`name`,r.`name`
        viewUser.setId((fields[0] == null) ? 0L:Long.parseLong(fields[0].toString()));
        viewUser.setUserName(fields[1] == null? "":String.valueOf(fields[1].toString()));
        viewUser.setName(fields[2] == null? "":String.valueOf(fields[2].toString()));
        viewUser.setTel(fields[3] == null? "":String.valueOf(fields[3].toString()));
        viewUser.setStatus(fields[4] == null? 0:Integer.parseInt(fields[4].toString()));
        viewUser.setDepart(fields[5] == null? "":String.valueOf(fields[5].toString()));
        viewUser.setRole(fields[6] == null? "":String.valueOf(fields[6].toString()));
        return viewUser;
    }

}
